package com.oneonefive.educationsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oneonefive.educationsystem.domain.Login;
import com.oneonefive.educationsystem.domain.User;
import com.oneonefive.educationsystem.mapper.UserMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String userAccount) throws UsernameNotFoundException {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("account", userAccount);
        User user = userMapper.selectOne(wrapper);
        if (Objects.isNull(user)) {
            log.info("登录失败");
        }
        Integer job = user.getJob();
        if (job == null) {
            log.info("用户权限为空");
            return new Login(user, null);
        }
        List<String> list = null;
        if (job == 0) {
            list = new ArrayList<>(Arrays.asList("admin", "teacher", "student", "tester"));
        }
        if (job == 1) {
            list = new ArrayList<>(List.of("teacher"));
        }
        if (job == 2) {
            list = new ArrayList<>(List.of("tester"));
        }
        if (job == 3) {
            list = new ArrayList<>(List.of("student"));
        }
        return new Login(user, list);
    }
}
